
/**
 *
 */
package com.serviceprovider.business.module.admin.transaction.utility;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.transaction.valueobjects.TransactionCSVHistory;

//~--- JDK imports ------------------------------------------------------------

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

/**
 * @author mrduongnv@gmail.com
 *
 */
public class HistoryDataTag implements Tag {
    private String headerCssClass = null;

    /** The history list. */
    private List<TransactionCSVHistory> historyList = null;

    /** The parent. */
    private Tag parent = null;

    /** The pc. */
    private PageContext pc            = null;
    private String      tableCssClass = null;
    private String      width         = null;

    @Override
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }

    @Override
    public int doStartTag() throws JspException {
        try {
            if (this.historyList != null) {
                this.pc.getOut().write(createHistoryTable(this.historyList));
            }
        } catch (IOException e) {
            throw new JspTagException("An IOException occurred.");
        }

        return SKIP_BODY;
    }

    private String createHistoryTable(List<TransactionCSVHistory> list) {
        StringBuilder builder = new StringBuilder();

        builder.append("<table ");

        if (!StringUtils.isEmpty(this.width)) {
            builder.append("width=\"");
            builder.append(this.width);
            builder.append("\" ");
        }

        if (!StringUtils.isEmpty(this.tableCssClass)) {
            builder.append("class=\"");
            builder.append(this.tableCssClass);
            builder.append("\"");
        }

        builder.append(">");
        builder.append("<tbody>");
        builder.append("<tr ");

        if (!StringUtils.isEmpty(this.headerCssClass)) {
            builder.append("class=\"");
            builder.append(this.headerCssClass);
            builder.append("\"");
        }

        builder.append(">");
        builder.append("<td>Id</td>");
        builder.append("<td>File Name</td>");
        builder.append("<td>File Location</td>");
        builder.append("<td>Created date</td>");
        builder.append("<td>Modified Date</td>");
        builder.append("<td>Has Error</td>");
        builder.append("<td>Processed</td>");
        builder.append("<td>Action</td>");
        builder.append("</tr>");

        for (TransactionCSVHistory transactionCSVHistory : list) {
            createTR(builder, transactionCSVHistory);
        }

        builder.append("</tbody>");
        builder.append("</table>");

        return builder.toString();
    }

    private void createTR(StringBuilder builder, TransactionCSVHistory transactionCSVHistory) {
        builder.append("<tr>");
        createTD(builder, transactionCSVHistory);
        builder.append("</tr>");
    }

    private void createTD(StringBuilder builder, TransactionCSVHistory transactionCSVHistory) {
        String contextPath = ((HttpServletRequest) this.pc.getRequest()).getContextPath();
        String url         = contextPath.concat("/admin/transaction/view_transactioncsv_record.htm?file_id=").concat(
                         transactionCSVHistory.getId().toString());
        String editImageUrl = "<img src=\"".concat(contextPath) + "/images/view-records.png\" alt=\"View All\" />";

        if (!transactionCSVHistory.isHasError() && transactionCSVHistory.isProcessed()) {
            createRecord(builder, transactionCSVHistory.getId().toString());
        } else {
            createRecord(builder, transactionCSVHistory.getId().toString(), url);
        }

        createRecord(builder, transactionCSVHistory.getFileName());
        createRecord(builder, transactionCSVHistory.getPath());
        createRecord(builder, format(transactionCSVHistory.getCreatedDate()));
        createRecord(builder, format(transactionCSVHistory.getModifiedDate()));
        createRecord(builder, transactionCSVHistory.isHasError()
                              ? "yes"
                              : "no");
        createRecord(builder, transactionCSVHistory.isProcessed()
                              ? "yes"
                              : "no");

        if (!transactionCSVHistory.isHasError() && transactionCSVHistory.isProcessed()) {
            createRecord(builder, editImageUrl);
        } else {
            createRecord(builder, editImageUrl, url);
        }
    }

    private String format(Date date) {
        if (date == null) {
            return "";
        }

        String           pattern = "MM/dd/yyyy hh:mm:ss";
        SimpleDateFormat format  = new SimpleDateFormat(pattern);

        return format.format(date);
    }

    private void createRecord(StringBuilder builder, String string) {
        builder.append("<td>");
        builder.append(string);
        builder.append("</td>");
    }

    private void createRecord(StringBuilder builder, String string, String url) {
        builder.append("<td>");
        builder.append("<a href=\"");
        builder.append(url);
        builder.append("\">");
        builder.append(string);
        builder.append("</a>");
        builder.append("</td>");
    }

    @Override
    public Tag getParent() {
        return this.parent;
    }

    @Override
    public void release() {
        this.pc             = null;
        this.parent         = null;
        this.historyList    = null;
        this.tableCssClass  = null;
        this.headerCssClass = null;
        this.width          = null;
    }

    @Override
    public void setPageContext(PageContext p) {
        this.pc = p;
    }

    @Override
    public void setParent(Tag t) {
        this.parent = t;
    }

    /**
     * @return the historyList
     */
    public List<TransactionCSVHistory> getHistoryList() {
        return this.historyList;
    }

    /**
     * @param historyList
     *            the historyList to set
     */
    public void setHistoryList(List<TransactionCSVHistory> historyList) {
        this.historyList = historyList;
    }

    /**
     * @return the tableCssClass
     */
    public String getTableCssClass() {
        return this.tableCssClass;
    }

    /**
     * @param tableCssClass
     *            the tableCssClass to set
     */
    public void setTableCssClass(String tableCssClass) {
        this.tableCssClass = tableCssClass;
    }

    /**
     * @return the headerCssClass
     */
    public String getHeaderCssClass() {
        return this.headerCssClass;
    }

    /**
     * @param headerCssClass
     *            the headerCssClass to set
     */
    public void setHeaderCssClass(String headerCssClass) {
        this.headerCssClass = headerCssClass;
    }

    /**
     * @return the width
     */
    public String getWidth() {
        return this.width;
    }

    /**
     * @param width
     *            the width to set
     */
    public void setWidth(String width) {
        this.width = width;
    }
}



